package web

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/julienschmidt/httprouter"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
	"notabug.org/ropemann/imabon/src/common"
)

type WebTestSuite struct {
	suite.Suite
	board    string
	threadId string
	router   httprouter.Router
	server   httptest.Server
}

func (s *WebTestSuite) SetupSuite() {
	common.LoadConfig("../../../.env")
	s.board = "ani"
	s.threadId = "625b8273c84324aea98ce004"
	s.router = *httprouter.New()
	RegisterWebRoutes(&s.router, "../../../public/html/*.html")
	s.server = *httptest.NewServer(&s.router)
}

func (s *WebTestSuite) TestRegisterWebRoutes() {
	h, p, b := s.router.Lookup("GET", "/")

	assert.NotNil(s.T(), h)
	assert.Empty(s.T(), p)
	assert.False(s.T(), b)
}

func (s *WebTestSuite) TestIndex() {
	r, err := http.Get(fmt.Sprintf("%s/", s.server.URL))
	assert.Nil(s.T(), err)
	assert.Equal(s.T(), 200, r.StatusCode)
}

func (s *WebTestSuite) TestBoard() {
	r, err := http.Get(fmt.Sprintf("%s/b/%s", s.server.URL, s.board))
	assert.Nil(s.T(), err)
	assert.Equal(s.T(), 200, r.StatusCode)
}

func (s *WebTestSuite) TestThread() {
	r, err := http.Get(fmt.Sprintf("%s/b/%s/%s", s.server.URL, s.board, s.threadId))
	assert.Nil(s.T(), err)
	assert.Equal(s.T(), 200, r.StatusCode)
}

func (s *WebTestSuite) TestGenerateCaptcha() {
	r, err := http.Get(fmt.Sprintf("%s/generate-captcha", s.server.URL))
	assert.Nil(s.T(), err)
	assert.Equal(s.T(), 200, r.StatusCode)
}

func TestWebTestSuite(t *testing.T) {
	suite.Run(t, new(WebTestSuite))
}
